package com.wgl.gsc.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.CompleteMultipartUploadResult;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyun.oss.model.UploadFileRequest;
import com.aliyun.oss.model.UploadFileResult;
import com.wgl.gsc.common.SysInfo;
import com.wgl.gsc.common.Tool;

public class BaseOssService {

	private static String endpoint = "oss-cn-shenzhen.aliyuncs.com";
	private static String accessKeyId = "LTAIQYUt0ac2nCRZ";
	private static String accessKeySecret = "WHm4iMCC0ZFmy0PLEKXguY9YwsdxbI";
	private static String DEFAULT_BUCKET = "ali-oss-wgl";
	private static Properties OSS_PROTERTES = null;
	private static Logger logger = Logger.getLogger(BaseOssService.class);

	static {
		try {
			OSS_PROTERTES = new Properties();
			String separator = SysInfo.getFileSeparator();
			InputStream fis = BaseOssService.class.getClassLoader()
					.getResourceAsStream(separator + "jltech_oss.properties");// 属性文件流
			OSS_PROTERTES.load(fis);
			logger.info("读取OSS配置文件...");
			endpoint = OSS_PROTERTES.getProperty("endpoint");
			accessKeyId = OSS_PROTERTES.getProperty("accessKeyId");
			accessKeySecret = OSS_PROTERTES.getProperty("accessKeySecret");
			DEFAULT_BUCKET = OSS_PROTERTES.getProperty("DEFAULT_BUCKET");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected static String baseUpload(String uploadFileFullPath, String fileName) {
		return baseUpload(DEFAULT_BUCKET, uploadFileFullPath, fileName);
	}

	protected static String baseUpload(String bucketName, String uploadFileFullPath, String fileName) {
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		try {
			String bucket = Tool.validStr(bucketName) ? bucketName : DEFAULT_BUCKET;
			if (Tool.validStr(fileName, uploadFileFullPath)) {
				return "";
			}
			UploadFileRequest uploadFileRequest = new UploadFileRequest(bucket, fileName);
			uploadFileRequest.setUploadFile(uploadFileFullPath);
			uploadFileRequest.setTaskNum(5);
			uploadFileRequest.setPartSize(1024 * 1024 * 1);
			uploadFileRequest.setEnableCheckpoint(true);
			UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
			CompleteMultipartUploadResult multipartUploadResult = uploadResult.getMultipartUploadResult();
			String fileUUID = multipartUploadResult.getETag();
			if (Tool.validStr(fileUUID)) {
				logger.info(fileUUID);
				return fileUUID;
			}
		} catch (OSSException oe) {
			logger.info("Caught an OSSException, which means your request made it to OSS, "
					+ "but was rejected with an error response for some reason.");
			logger.info("Error Message: " + oe.getErrorCode());
			logger.info("Error Code:       " + oe.getErrorCode());
			logger.info("Request ID:      " + oe.getRequestId());
			logger.info("Host ID:           " + oe.getHostId());
		} catch (ClientException ce) {
			logger.info("Caught an ClientException, which means the client encountered "
					+ "a serious internal problem while trying to communicate with OSS, "
					+ "such as not being able to access the network.");
			logger.info("Error Message: " + ce.getMessage());
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			ossClient.shutdown();
		}
		return "";
	}

	protected static String baseUpload(String fileName, InputStream inputStream) {
		return baseUpload(DEFAULT_BUCKET, fileName, inputStream);
	}

	protected static String baseUpload(String bucketName, String fileName, InputStream inputStream) {
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		ResponseMessage responseMsg = null;
		try {
			String bucket = Tool.validStr(bucketName) ? bucketName : DEFAULT_BUCKET;
			if (Tool.validStr(fileName) || inputStream == null) {
				return "";
			}
			PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, fileName, inputStream);
			PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
			String fileMD5 = putObjectResult.getETag();
			responseMsg = putObjectResult.getResponse();
			if (responseMsg == null && Tool.validStr(fileMD5)) {
				return fileMD5;
			}
		} catch (OSSException oe) {
			logger.info("Caught an OSSException, which means your request made it to OSS, "
					+ "but was rejected with an error response for some reason.");
			logger.info("Error Message: " + oe.getErrorCode());
			logger.info("Error Code:       " + oe.getErrorCode());
			logger.info("Request ID:      " + oe.getRequestId());
			logger.info("Host ID:           " + oe.getHostId());
		} catch (ClientException ce) {
			logger.info("Caught an ClientException, which means the client encountered "
					+ "a serious internal problem while trying to communicate with OSS, "
					+ "such as not being able to access the network.");
			logger.info("Error Message: " + ce.getMessage());
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			ossClient.shutdown();
		}
		return "";
	}

}
